package com.daidai.service.customer.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.daidai.dao.CustomerMapper;
import com.daidai.dao.GradeMapper;
import com.daidai.domain.Common;
import com.daidai.domain.Customer;
import com.daidai.domain.Grade;
import com.daidai.service.customer.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    CustomerMapper customerMapper;

    @Autowired
    GradeMapper gradeMapper;

    @Override
    public Common getAllCus() {
        try {
            List<Customer> allCus = customerMapper.getAllCus();
            return Common.success("查询成功", allCus);
        }catch (Exception exception){
            return Common.error(500, "查询失败");
        }
    }

    @Override
    public Common getNoCus() {
        List<Customer> list = customerMapper.selectList(new QueryWrapper<Customer>().eq("isdel", 0));
        return Common.success("查询成功", list);
    }

    @Override
    public Common getIsDel() {
        try{
            return Common.success("查询成功",customerMapper.selectList(new QueryWrapper<Customer>().eq("isdel",1)));
        }catch (Exception e){
            return Common.error(500,"查询失败");
        }
    }

    @Override
    public Boolean RegCus(Customer customer) {
        int insert = customerMapper.insert(customer);
        if(insert==1){
            return true;
        }
        return false;
    }

    @Override
    public Common delCus(int cid) {
        try{
            int i = customerMapper.deleteById(cid);
            if(i==0){
                return Common.success("删除成功");
            }
        }
        catch (Exception e){
            return Common.error(500,"删除出错");
        }
        return Common.error(500,"删除失败");
    }

    @Override
    public Common getCusByParam(String param, Long tel) {
        try{
            Customer one = customerMapper.selectOne(new QueryWrapper<Customer>().eq(param,tel));
            if(ObjectUtil.isNull(one)){
                return Common.error(500,"查询失败");
            }
            Integer grade = one.getGrade();
            Grade grade1 = gradeMapper.selectById(grade);
            one.setGrade1(grade1);

            return Common.success("查询成功",one);
        } catch (Exception e){
            return Common.error(500,"查询出错");
        }
    }

    @Override
    public Common updateCus(Customer customer) {
        try{
            return Common.success("修改成功",customerMapper.update(customer,new QueryWrapper<Customer>().eq("cid",customer.getCid())));
        }catch (Exception e){
            return Common.error(500,"修改出错");
        }
    }
}
